package src.Players;

import src.Main;

public class PlayerDuel {
	private Player p;

	public PlayerDuel(Player _p) {
		p = _p;
	}
	
	public void duelPlayer() {
		if(p == null) {
			return;
		}
		if(p.inDuel == true || p.inTrade == true) {	
			return;
		}	
		Player plr = Main.m.ge.players[p.duelPlayer];
		p.duelingWith = plr.playerIndex;
        	if(p.faceToRequest != (p.duelPlayer + 32768)) {
           		p.requestFaceTo(p.duelPlayer + 32768);
        	}
		if(plr != null) {
			if(Main.m.ge.getDistance(plr.absX, plr.absY, p.absX, p.absY) != 1) {
                    			return;
                	}	
			if(plr.duelingPlayer == true && plr.duelingWith == p.playerIndex) {
				plr.duelingPlayer = false;
				plr.inDuel = true;
				p.duelingPlayer = false;
				p.inDuel = true;
				plr.frame.showInterface(plr, 6572, 3213);
				p.frame.showInterface(p, 6572, 3213);
				p.frame.setText126(p, "Dueling with: "+plr.username, 6575);
				p.frame.setText126(p, "", 6684);
				plr.frame.setText126(plr, "Dueling with: "+p.username, 6575);
				plr.frame.setText126(plr, "", 6684);
			} else {
				p.frame.sendMessage(p, "Duel request sent...");
				plr.frame.sendMessage(plr, p.username+" wants to duel.");
			}			
		}
	}

	public boolean appendItem(Player p, int itemId, int fromSlot, int amt) {
		Player plr = Main.m.ge.players[p.duelPlayer];
		if(p == null || fromSlot < 0 || amt <= 0) {
			return false;
		}
		PlayerChecks pc = new PlayerChecks(p);
		if(!pc.playerHasItem(p, itemId)) {
			return false;
		}
		PlayerItems pi = new PlayerItems();
		if(Main.m.ge.item.stackable(itemId) || Main.m.ge.item.noted(itemId)) {
			for(int item : p.offeredItems) {
				if(item == itemId) {
					pi.deleteItem(p, itemId, pi.getItemSlot(p, itemId), amt);
					break;
				}
			}
		} else {
			for(int a = 0; a < amt; a++) {
				p.offeredItems.add(itemId);
				pi.deleteItem(p, itemId, pi.getItemSlot(p, itemId), amt);
			}
		}
		p.frame.resetItems(p, 3323);
		plr.frame.resetItems(p, 3323);
		p.frame.setText126(p, "", 6571);
		plr.frame.setText126(plr, "", 6571);
		return true;	
	}

	public int freeSlots(Player p) {
		if(p == null) {
			return -1;
		}
		int s = 0;
		for(int i = 0; i < 28; i++) {
		}
		return s;
	}

	public void acceptFirstScreen(Player p) {
		if(p == null) {
			return;
		}
		Player plr = Main.m.ge.players[p.duelPlayer];
		p.duelingWith = plr.playerIndex;
		if(plr != null) {
			p.acceptedFirstScreen = true;
			p.frame.setText126(p, "Waiting on other player...", 6571);
			plr.frame.setText126(plr, p.username+" has accepted.", 6571);
			if(plr.acceptedFirstScreen == true) {
				p.acceptedFirstScreen = false;
				plr.acceptedFirstScreen = false;
				p.frame.removeAllWindows(p);
				plr.frame.removeAllWindows(plr);
				p.frame.showInterface(p, 6412, 3213);
				plr.frame.showInterface(plr, 6412, 3213);			
			}
		}
	}

	public void acceptSecondScreen(Player p) {
		if(p == null) {
			return;
		}
		Player plr = Main.m.ge.players[p.duelPlayer];
		p.duelingWith = plr.playerIndex;
		if(plr != null) {
			p.acceptedSecondScreen = true;
			if(plr.acceptedSecondScreen == true) {
				p.acceptedSecondScreen = false;
				plr.acceptedSecondScreen = false;
				p.frame.removeAllWindows(p);
				plr.frame.removeAllWindows(plr);
				beginDuel(p);
			}
		}
	}

	public void beginDuel(Player p) {
		
	}
}